home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / sed-2_00.lha / sed-2.00 / rx.h < prev    next >
C/C++ Source or Header  |  1993-07-15  |  33KB  |  1,036 lines

  1. #if !defined(RXH) || defined(RX_WANT_SE_DEFS)
  2. #define RXH
  3.  
  4. /*    Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this software; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19. /*  t. lord    Wed Sep 23 18:20:57 1992    */
  20.  
  21.  
  22.  
  23.  
  24. #ifndef RX_WANT_SE_DEFS
  25.  
  26. /* This page: Bitsets */
  27.  
  28. #ifndef RX_subset
  29. typedef unsigned int RX_subset;
  30. #define RX_subset_bits    (32)
  31. #define RX_subset_mask    (RX_subset_bits - 1)
  32. #endif
  33.  
  34. typedef RX_subset * rx_Bitset;
  35.  
  36. #ifdef __STDC__
  37. typedef void (*rx_bitset_iterator) (void *, int member_index);
  38. #else
  39. typedef void (*rx_bitset_iterator) ();
  40. #endif
  41.  
  42. #define rx_bitset_subset(N)  ((N) / RX_subset_bits)
  43. #define rx_bitset_subset_val(B,N)  ((B)[rx_bitset_subset(N)])
  44. #define RX_bitset_access(B,N,OP) \
  45.   ((B)[rx_bitset_subset(N)] OP rx_subset_singletons[(N) & RX_subset_mask])
  46. #define RX_bitset_member(B,N)   RX_bitset_access(B, N, &)
  47. #define RX_bitset_enjoin(B,N)   RX_bitset_access(B, N, |=)
  48. #define RX_bitset_remove(B,N)   RX_bitset_access(B, N, &= ~)
  49. #define RX_bitset_toggle(B,N)   RX_bitset_access(B, N, ^= )
  50. #define rx_bitset_numb_subsets(N) (((N) + RX_subset_bits - 1) / RX_subset_bits)
  51. #define rx_sizeof_bitset(N)    (rx_bitset_numb_subsets(N) * sizeof(RX_subset))
  52.  
  53.  
  54.  
  55. /* This page: Splay trees. */
  56.  
  57. #ifdef __STDC__
  58. typedef int (*rx_sp_comparer) (void * a, void * b);
  59. #else
  60. typedef int (*rx_sp_comparer) ();
  61. #endif
  62.  
  63. struct rx_sp_node 
  64. {
  65.   void * key;
  66.   void * data;
  67.   struct rx_sp_node * kids[2];
  68. };
  69.  
  70. #ifdef __STDC__
  71. typedef void (*rx_sp_key_data_freer) (struct rx_sp_node *);
  72. #else
  73. typedef void (*rx_sp_key_data_freer) ();
  74. #endif
  75.  
  76.  
  77. /* giant inflatable hash trees */
  78.  
  79. struct rx_hash_item
  80. {
  81.   struct rx_hash_item * next_same_hash;
  82.   struct rx_hash * table;
  83.   unsigned long hash;
  84.   void * data;
  85.   void * binding;
  86. };
  87.  
  88. struct rx_hash
  89. {
  90.   struct rx_hash * parent;
  91.   int refs;
  92.   struct rx_hash * children[13];
  93.   struct rx_hash_item * buckets [13];
  94.   int bucket_size [13];
  95. };
  96.  
  97. struct rx_hash_rules;
  98.  
  99. #ifdef __STDC__
  100. /* should return like == */
  101. typedef int (*rx_hash_eq)(void *, void *);
  102. typedef struct rx_hash * (*rx_alloc_hash)(struct rx_hash_rules *);
  103. typedef void (*rx_free_hash)(struct rx_hash *,
  104.                 struct rx_hash_rules *);
  105. typedef struct rx_hash_item * (*rx_alloc_hash_item)(struct rx_hash_rules *,
  106.                             void *);
  107. typedef void (*rx_free_hash_item)(struct rx_hash_item *,
  108.                  struct rx_hash_rules *);
  109. #else
  110. typedef int (*rx_hash_eq)();
  111. typedef struct rx_hash * (*rx_alloc_hash)();
  112. typedef void (*rx_free_hash)();
  113. typedef struct rx_hash_item * (*rx_alloc_hash_item)();
  114. typedef void (*rx_free_hash_item)();
  115. #endif
  116.  
  117. struct rx_hash_rules 
  118. {
  119.   rx_hash_eq eq;
  120.   rx_alloc_hash hash_alloc;
  121.   rx_free_hash free_hash;    /* oh, shut up don */
  122.   rx_alloc_hash_item hash_item_alloc;
  123.   rx_free_hash_item free_hash_item;
  124. };
  125.  
  126.  
  127.  
  128. /* Matchers decide what to do by examining a series of these.
  129.  * Instruction types are described below.
  130.  */
  131. struct rx_inx 
  132. {
  133.   void * inx;
  134.   void * data;
  135.   void * data_2;
  136.   void * fnord;
  137. };
  138.  
  139. /* Struct RX holds a compiled regular expression - that is, an nfa ready to be
  140.  * converted on demand to a more efficient nfa.  This is for the low level interface.
  141.  * The high-level interface incloses this in a `struct re_pattern_buffer'.
  142.  */
  143.  
  144. struct rx_cache;
  145.  
  146. struct rx
  147. {
  148.   int rx_id;        /* Every edition numbered and signed by eclose_nfa. */
  149.  
  150.   struct rx_cache * cache;  /* Where superstates come from */
  151.  
  152.   /* Every regex defines the size of its own character set. */
  153.   int local_cset_size;
  154.  
  155.   void * buffer;        /* Malloced memory for the nfa. */
  156.   unsigned long allocated;    /* Size of that memory. */
  157.  
  158.   /* How much buffer space to save for external uses.  After compilation,
  159.    * this space will be available at (buffer + allocated - reserved)
  160.    */
  161.   unsigned long reserved;
  162.  
  163.   /* --------- The remaining fields are for internal use only. --------- */
  164.   /* --------- But! they should be initialized to 0.           --------- */
  165.   /* NODEC is the number of nodes in the NFA with non-epsilon
  166.    * orx transitions. 
  167.    */
  168.   int nodec;
  169.  
  170.   /* EPSNODEC is the number of nodes with only epsilon (orx) transitions. */
  171.   int epsnodec;
  172.  
  173.   /* The sum of NODEC & EPSNODEC is the total number of states in the
  174.    * compiled NFA.
  175.    */
  176.  
  177.   /* side_effect_progs temporarily holds a tree of side effect lists. */
  178.   struct rx_hash se_list_memo;
  179.  
  180.   /* A memo for sets of states in the possible_future lists of an nfa: */
  181.   struct rx_hash set_list_memo;
  182.  
  183.   /* The instruction table is indexed by the enum of instructions defined in 
  184.    * rxrun.h.  The values in the table are used to fill in the `inx'
  185.    * slot of instruction frames (see rxrun.h).
  186.    */
  187.   void ** instruction_table;
  188.   struct rx_nfa_state *nfa_states;
  189.   struct rx_nfa_state *start;
  190. };
  191.  
  192. /* An RX NFA may contain epsilon edges labeled with side effects.
  193.  * These side effects represent match actions that can not normally be
  194.  * defined in a `pure' NFA; for example, recording the location at
  195.  * which a paren is crossed in a register structure.  
  196.  *
  197.  * A matcher is supposed to find a particular path
  198.  * through the NFA (such as leftmost-longest), and then to execute the
  199.  * side effects along that path.  Operationally, the space of paths is
  200.  * searched and side effects are carried out incrementally, and with
  201.  * backtracking.
  202.  *
  203.  * As the NFA is manipulated during matching sets of side effects.
  204.  * Simple lists are used to hold side effect lists. 
  205.  */
  206.  
  207. typedef void * rx_side_effect;
  208.  
  209. struct rx_se_list 
  210. {
  211.   rx_side_effect car;
  212.   struct rx_se_list * cdr;
  213. };
  214.  
  215.  
  216.  
  217. /* Struct rexp_node holds an expression tree that represents a regexp.
  218.  * In this expression tree, every node has a type, and some parameters
  219.  * appropriate to that type.
  220.  */
  221.  
  222. enum rexp_node_type
  223. {
  224.   r_cset,            /* Match from a character set. `a' or `[a-z]'*/
  225.   r_concat,            /* Concat two regexps.   `ab' */
  226.   r_alternate,            /* Choose one of two regexps. `a\|b' */
  227.   r_opt,            /* Optional regexp. `a?' */
  228.   r_star,            /* Repeated regexp. `a*' */
  229.   r_2phase_star,        /* hard to explain */
  230.   r_side_effect,        /* Matches the empty string, but
  231.                  * implies that a side effect must
  232.                  * take place.  These nodes are used
  233.                  * by the parser to implement parens,
  234.                  * backreferences etc.
  235.                  */
  236.  
  237.   r_data            /* R_DATA is soley for the convenience
  238.                  * of parsers or other rexp
  239.                  * transformers that want to
  240.                  * (temporarily) introduce new node
  241.                  * types in rexp structures.  These
  242.                  * must be eliminated
  243.                      * by the time build_nfa is called.
  244.                    */
  245. };
  246.  
  247. struct rexp_node
  248. {
  249.   enum rexp_node_type type;
  250.   union
  251.   {
  252.     rx_Bitset cset;
  253.     rx_side_effect side_effect;
  254.     struct
  255.       {
  256.     struct rexp_node *left;
  257.     struct rexp_node *right;
  258.       } pair;
  259.     void * data;
  260.   } params;
  261. };
  262.  
  263.  
  264.  
  265. /* This defines the structure of the NFA into which rexps are compiled. */
  266.  
  267. struct rx_nfa_state
  268. {
  269.   int id;        
  270.   struct rx_nfa_edge *edges;
  271.   struct rx_possible_future *futures;
  272.   unsigned int is_final:1;
  273.   unsigned int is_start:1;
  274.   unsigned int eclosure_needed:1;
  275.   struct rx_nfa_state *next;
  276.   unsigned int mark:1;
  277. };
  278.  
  279. enum rx_nfa_etype
  280. {
  281.   ne_cset,
  282.   ne_epsilon,
  283.   ne_side_effect        /* A special kind of epsilon. */
  284. };
  285.  
  286. struct rx_nfa_edge
  287. {
  288.   struct rx_nfa_edge *next;
  289.   enum rx_nfa_etype type;
  290.   struct rx_nfa_state *dest;
  291.   union
  292.   {
  293.     rx_Bitset cset;
  294.     rx_side_effect side_effect;
  295.   } params;
  296. };
  297.  
  298. struct rx_nfa_state_set
  299. {
  300.   struct rx_nfa_state * car;
  301.   struct rx_nfa_state_set * cdr;
  302. };
  303.  
  304. struct rx_possible_future
  305. {
  306.   struct rx_possible_future *next;
  307.   struct rx_se_list * effects;
  308.   struct rx_nfa_state_set * destset;
  309. };
  310.  
  311.  
  312.  
  313. enum rx_opcode
  314. {
  315.   /* 
  316.    * BACKTRACK_POINT is invoked when a transition results in more
  317.    * than one possible future.
  318.    *
  319.    * There is one occurence of this instruction per transition_class
  320.    * structure; that occurence is only ever executed if the 
  321.    * transition_class contains a list of more than 1 edge.
  322.    */
  323.   rx_backtrack_point = 0,    /* data is (struct transition_class *) */
  324.  
  325.   /* 
  326.    * RX_DO_SIDE_EFFECTS evaluates the side effects of an epsilon path.
  327.    * There is one occurence of this instruction per rx_distinct_future.
  328.    * This instruction is skipped if a rx_distinct_future has no side effects.
  329.    */
  330.   rx_do_side_effects = rx_backtrack_point + 1,
  331.   /* data is (struct rx_distinct_future *) */
  332.  
  333.   /* 
  334.    * RX_CACHE_MISS instructions are stored in rx_distinct_futures whose
  335.    * destination superstate has been reclaimed (or was never built).
  336.    * It recomputes the destination superstate.
  337.    * RX_CACHE_MISS is also stored in a superstate transition table before
  338.    * any of its edges have been built.
  339.    */
  340.   rx_cache_miss = rx_do_side_effects + 1,
  341.   /* data is (struct rx_distinct_future *) */
  342.  
  343.   /* 
  344.    * RX_NEXT_CHAR is called to consume the next character and take the
  345.    * corresponding transition.  This is the only instruction that uses 
  346.    * the DATA field of the instruction frame instead of DATA_2.
  347.    * (see EXPLORE_FUTURE in regex.c).
  348.    */
  349.   rx_next_char = rx_cache_miss + 1, /* data is (struct superstate *) */
  350.  
  351.   /* RX_BACKTRACK indicates that a transition fails.
  352.    */
  353.   rx_backtrack = rx_next_char + 1, /* no data */
  354.  
  355.   /* 
  356.    * RX_ERROR_INX is stored only in places that should never be executed.
  357.    */
  358.   rx_error_inx = rx_backtrack + 1, /* Not supposed to occur. */
  359.  
  360.   rx_num_instructions = rx_error_inx + 1
  361. };
  362.  
  363. /* An id_instruction_table holds the values stored in instruction
  364.  * frames.  The table is indexed by the enums declared above.
  365.  */
  366. extern void * rx_id_instruction_table[rx_num_instructions];
  367.  
  368. #if 0                /* Already declared way above. */
  369. /*  If the instruction is `rx_next_char' then data is valid.  Otherwise it's 0
  370.  *  and data_2 is valid.
  371.  */
  372. struct rx_inx 
  373. {
  374.   void * inx;
  375.   void * data;
  376.   void * data_2;
  377. };
  378. #endif
  379.  
  380.  
  381. #ifndef RX_TAIL_ARRAY
  382. #define RX_TAIL_ARRAY  1
  383. #endif
  384.  
  385. /* A superstate corresponds to a set of nfa states.  Those sets are
  386.  * represented by STRUCT RX_SUPERSET.  The constructors
  387.  * guarantee that only one (shared) structure is created for a given set.
  388.  */
  389. struct rx_superset
  390. {
  391.   int refs;
  392.   struct rx_nfa_state * car;    /* May or may not be a valid addr. */
  393.   int id;            /* == car->id for the initial value of *car */
  394.   struct rx_superset * cdr;    /* May be NULL or a live or semifreed super*/
  395.  
  396.   /* If the corresponding superstate exists: */
  397.   struct rx_superstate * superstate;
  398.  
  399.   struct rx_hash_item hash_item;
  400. };
  401.  
  402. #define rx_protect_superset(RX,CON) (++(CON)->refs)
  403.  
  404. /* Every character occurs in at most one super edge per super-state.
  405.  * But, that edge might have more than one option, indicating a point
  406.  * of non-determinism. 
  407.  */
  408. struct rx_super_edge
  409. {
  410.   struct rx_super_edge *next;
  411.   struct rx_inx rx_backtrack_frame;
  412.   int cset_size;
  413.   rx_Bitset cset;
  414.   struct rx_distinct_future *options;
  415. };
  416.  
  417. /* A superstate is a set of nfa states (RX_SUPERSET) along
  418.  * with a transition table.  Superstates are built on demand and reclaimed
  419.  * without warning.  To protect a superstate, use LOCK_SUPERSTATE.
  420.  */
  421. struct rx_superstate
  422. {
  423.   int rx_id;
  424.   int locks;
  425.   struct rx_superstate * next_recyclable;
  426.   struct rx_superstate * prev_recyclable;
  427.   struct rx_distinct_future * transition_refs;
  428.   struct rx_superset * contents;
  429.   struct rx_super_edge * edges;
  430.   int is_semifree;
  431.   int trans_size;
  432.   struct rx_inx transitions[RX_TAIL_ARRAY]; /* cset sized */
  433. };
  434.  
  435. struct rx_distinct_future
  436. {
  437.   struct rx_distinct_future * next_same_super_edge;
  438.   struct rx_distinct_future * next_same_dest;
  439.   struct rx_distinct_future * prev_same_dest;
  440.   struct rx_superstate * present;    /* source state */
  441.   struct rx_superstate * future;    /* destination state */
  442.   struct rx_super_edge * edge;
  443.   struct rx_inx future_frame;
  444.   struct rx_inx side_effects_frame;
  445.   struct rx_se_list * effects;
  446. };
  447.  
  448. #define rx_lock_superstate(R,S)  ((S)->locks++)
  449. #define rx_unlock_superstate(R,S) (--(S)->locks)
  450.  
  451.  
  452. /* This page destined for rx.h */
  453.  
  454. struct rx_blocklist
  455. {
  456.   struct rx_blocklist * next;
  457.   int bytes;
  458. };
  459.  
  460. struct rx_freelist
  461. {
  462.   struct rx_freelist * next;
  463. };
  464.  
  465. struct rx_cache;
  466.  
  467. #ifdef __STDC__
  468. typedef void (*rx_morecore_fn)(struct rx_cache *);
  469. #else
  470. typedef void (*rx_morecore_fn)();
  471. #endif
  472.  
  473. struct rx_cache
  474. {
  475.   struct rx_hash_rules superset_hash_rules;
  476.  
  477.   /* Objects are allocated by incrementing a pointer that 
  478.    * scans across rx_blocklists.
  479.    */
  480.   struct rx_blocklist * memory;
  481.   struct rx_blocklist * memory_pos;
  482.   int bytes_left;
  483.   char * memory_addr;
  484.   rx_morecore_fn morecore;
  485.  
  486.   /* Freelists. */
  487.   struct rx_freelist * free_superstates;
  488.   struct rx_freelist * free_transition_classes;
  489.   struct rx_freelist * free_discernable_futures;
  490.   struct rx_freelist * free_supersets;
  491.   struct rx_freelist * free_hash;
  492.  
  493.   /* Two sets of superstates -- those that are semifreed, and those
  494.    * that are being used.
  495.    */
  496.   struct rx_superstate * lru_superstate;
  497.   struct rx_superstate * semifree_superstate;
  498.  
  499.   struct rx_superset * empty_superset;
  500.  
  501.   int superstates;
  502.   int semifree_superstates;
  503.   int hits;
  504.   int misses;
  505.   int superstates_allowed;
  506.  
  507.   int local_cset_size;
  508.   void ** instruction_table;
  509.  
  510.   struct rx_hash superset_table;
  511. };
  512.  
  513.  
  514.  
  515. /* regex.h
  516.  * 
  517.  * The remaining declarations replace regex.h.
  518.  */
  519.  
  520. /* This is an array of error messages corresponding to the error codes.
  521.  */
  522. extern const char *re_error_msg[];
  523.  
  524. /* If any error codes are removed, changed, or added, update the
  525.    `re_error_msg' table in regex.c.  */
  526. typedef enum
  527. {
  528.   REG_NOERROR = 0,    /* Success.  */
  529.   REG_NOMATCH,        /* Didn't find a match (for regexec).  */
  530.  
  531.   /* POSIX regcomp return error codes.  (In the order listed in the
  532.      standard.)  */
  533.   REG_BADPAT,        /* Invalid pattern.  */
  534.   REG_ECOLLATE,        /* Not implemented.  */
  535.   REG_ECTYPE,        /* Invalid character class name.  */
  536.   REG_EESCAPE,        /* Trailing backslash.  */
  537.   REG_ESUBREG,        /* Invalid back reference.  */
  538.   REG_EBRACK,        /* Unmatched left bracket.  */
  539.   REG_EPAREN,        /* Parenthesis imbalance.  */ 
  540.   REG_EBRACE,        /* Unmatched \{.  */
  541.   REG_BADBR,        /* Invalid contents of \{\}.  */
  542.   REG_ERANGE,        /* Invalid range end.  */
  543.   REG_ESPACE,        /* Ran out of memory.  */
  544.   REG_BADRPT,        /* No preceding re for repetition op.  */
  545.  
  546.   /* Error codes we've added.  */
  547.   REG_EEND,        /* Premature end.  */
  548.   REG_ESIZE,        /* Compiled pattern bigger than 2^16 bytes.  */
  549.   REG_ERPAREN        /* Unmatched ) or \); not returned from regcomp.  */
  550. } reg_errcode_t;
  551.  
  552. /* The regex.c support, as a client of rx, defines a set of possible
  553.  * side effects that can be added to the edge lables of nfa edges.
  554.  * Here is the list of sidef effects in use.
  555.  */
  556.  
  557. enum re_side_effects
  558. {
  559. #define RX_WANT_SE_DEFS 1
  560. #undef RX_DEF_SE
  561. #undef RX_DEF_CPLX_SE
  562. #define RX_DEF_SE(IDEM, NAME, VALUE)          NAME VALUE,
  563. #define RX_DEF_CPLX_SE(IDEM, NAME, VALUE)     NAME VALUE,
  564. #include "rx.h"
  565. #undef RX_DEF_SE
  566. #undef RX_DEF_CPLX_SE
  567. #undef RX_WANT_SE_DEFS
  568.    re_floogle_flap = 65533
  569. };
  570.  
  571. /* These hold paramaters for the kinds of side effects that are possible
  572.  * in the supported pattern languages.  These include things like the 
  573.  * numeric bounds of {} operators and the index of paren registers for 
  574.  * subexpression measurement or backreferencing.
  575.  */
  576. struct re_se_params
  577. {
  578.   enum re_side_effects se;
  579.   int op1;
  580.   int op2;
  581. };
  582.  
  583. typedef unsigned reg_syntax_t;
  584.  
  585. struct re_pattern_buffer
  586. {
  587.   struct rx rx;
  588.   reg_syntax_t syntax;        /* See below for syntax bit definitions. */
  589.  
  590.   unsigned int no_sub:1;    /* If set, don't  return register offsets. */
  591.   unsigned int not_bol:1;    /* If set, the anchors ('^' and '$') don't */
  592.   unsigned int not_eol:1;    /*     match at the ends of the string.  */  
  593.   unsigned int newline_anchor:1;/* If true, an anchor at a newline matches.*/
  594.   unsigned int least_subs:1;    /* If set, and returning registers, return
  595.                  * as few values as possible.  Only 
  596.                  * backreferenced groups and group 0 (the whole
  597.                  * match) will be returned.
  598.                  */
  599.  
  600.   /* If true, this says that the matcher should keep registers on its
  601.    * backtracking stack.  For many patterns, we can easily determine that
  602.    * this isn't necessary.
  603.    */
  604.   unsigned int match_regs_on_stack:1;
  605.   unsigned int search_regs_on_stack:1;
  606.  
  607.   /* is_anchored and begbuf_only are filled in by rx_compile. */
  608.   unsigned int is_anchored:1;    /* Anchorded by ^? */
  609.   unsigned int begbuf_only:1;    /* Anchored to char position 0? */
  610.  
  611.   
  612.   /* If REGS_UNALLOCATED, allocate space in the `regs' structure
  613.    * for `max (RE_NREGS, re_nsub + 1)' groups.
  614.    * If REGS_REALLOCATE, reallocate space if necessary.
  615.    * If REGS_FIXED, use what's there.  
  616.    */
  617. #define REGS_UNALLOCATED 0
  618. #define REGS_REALLOCATE 1
  619. #define REGS_FIXED 2
  620.   unsigned int regs_allocated:2;
  621.  
  622.   
  623.   /* Either a translate table to apply to all characters before
  624.    * comparing them, or zero for no translation.  The translation
  625.    * is applied to a pattern when it is compiled and to a string
  626.    * when it is matched.
  627.    */
  628.   char * translate;
  629.  
  630.   /* If this is a valid pointer, it tells rx not to store the extents of 
  631.    * certain subexpressions (those corresponding to non-zero entries).
  632.    * Passing 0x1 is the same as passing an array of all ones.  Passing 0x0
  633.    * is the same as passing an array of all zeros.
  634.    * The array should contain as many entries as their are subexps in the 
  635.    * regexp.
  636.    */
  637.   char * syntax_parens;
  638.  
  639.     /* Number of subexpressions found by the compiler.  */
  640.   size_t re_nsub;
  641.  
  642.   void * buffer;        /* Malloced memory for the nfa. */
  643.   unsigned long allocated;    /* Size of that memory. */
  644.  
  645.   /* Pointer to a fastmap, if any, otherwise zero.  re_search uses
  646.    * the fastmap, if there is one, to skip over impossible
  647.    * starting points for matches.  */
  648.   char *fastmap;
  649.  
  650.   unsigned int fastmap_accurate:1; /* These three are internal. */
  651.   unsigned int can_match_empty:1;  
  652.   struct rx_nfa_state * start;    /* The nfa starting state. */
  653.  
  654.   /* This is the list of iterator bounds for {lo,hi} constructs.
  655.    * The memory pointed to is part of the rx->buffer.
  656.    */
  657.   struct re_se_params *se_params;
  658.  
  659.   /* This is a bitset representation of the fastmap.
  660.    * This is a true fastmap that already takes the translate
  661.    * table into account.
  662.    */
  663.   rx_Bitset fastset;
  664. };
  665.  
  666. /* Type for byte offsets within the string.  POSIX mandates this.  */
  667. typedef int regoff_t;
  668.  
  669. /* This is the structure we store register match data in.  See
  670.    regex.texinfo for a full description of what registers match.  */
  671. struct re_registers
  672. {
  673.   unsigned num_regs;
  674.   regoff_t *start;
  675.   regoff_t *end;
  676. };
  677.  
  678. typedef struct re_pattern_buffer regex_t;
  679.  
  680. /* POSIX specification for registers.  Aside from the different names than
  681.    `re_registers', POSIX uses an array of structures, instead of a
  682.    structure of arrays.  */
  683. typedef struct
  684. {
  685.   regoff_t rm_so;  /* Byte offset from string's start to substring's start.  */
  686.   regoff_t rm_eo;  /* Byte offset from string's start to substring's end.  */
  687. } regmatch_t;
  688.  
  689.  
  690. /* The following bits are used to determine the regexp syntax we
  691.    recognize.  The set/not-set meanings are chosen so that Emacs syntax
  692.    remains the value 0.  The bits are given in alphabetical order, and
  693.    the definitions shifted by one from the previous bit; thus, when we
  694.    add or remove a bit, only one other definition need change.  */
  695.  
  696. /* If this bit is not set, then \ inside a bracket expression is literal.
  697.    If set, then such a \ quotes the following character.  */
  698. #define RE_BACKSLASH_ESCAPE_IN_LISTS (1)
  699.  
  700. /* If this bit is not set, then + and ? are operators, and \+ and \? are
  701.      literals. 
  702.    If set, then \+ and \? are operators and + and ? are literals.  */
  703. #define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
  704.  
  705. /* If this bit is set, then character classes are supported.  They are:
  706.      [:alpha:], [:upper:], [:lower:],  [:digit:], [:alnum:], [:xdigit:],
  707.      [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
  708.    If not set, then character classes are not supported.  */
  709. #define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
  710.  
  711. /* If this bit is set, then ^ and $ are always anchors (outside bracket
  712.      expressions, of course).
  713.    If this bit is not set, then it depends:
  714.         ^  is an anchor if it is at the beginning of a regular
  715.            expression or after an open-group or an alternation operator;
  716.         $  is an anchor if it is at the end of a regular expression, or
  717.            before a close-group or an alternation operator.  
  718.  
  719.    This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
  720.    POSIX draft 11.2 says that * etc. in leading positions is undefined.
  721.    We already implemented a previous draft which made those constructs
  722.    invalid, though, so we haven't changed the code back.  */
  723. #define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
  724.  
  725. /* If this bit is set, then special characters are always special
  726.      regardless of where they are in the pattern.
  727.    If this bit is not set, then special characters are special only in
  728.      some contexts; otherwise they are ordinary.  Specifically, 
  729.      * + ? and intervals are only special when not after the beginning,
  730.      open-group, or alternation operator.  */
  731. #define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
  732.  
  733. /* If this bit is set, then *, +, ?, and { cannot be first in an re or
  734.      immediately after an alternation or begin-group operator.  */
  735. #define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
  736.  
  737. /* If this bit is set, then . matches newline.
  738.    If not set, then it doesn't.  */
  739. #define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
  740.  
  741. /* If this bit is set, then . doesn't match NUL.
  742.    If not set, then it does.  */
  743. #define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
  744.  
  745. /* If this bit is set, nonmatching lists [^...] do not match newline.
  746.    If not set, they do.  */
  747. #define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
  748.  
  749. /* If this bit is set, either \{...\} or {...} defines an
  750.      interval, depending on RE_NO_BK_BRACES. 
  751.    If not set, \{, \}, {, and } are literals.  */
  752. #define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
  753.  
  754. /* If this bit is set, +, ? and | aren't recognized as operators.
  755.    If not set, they are.  */
  756. #define RE_LIMITED_OPS (RE_INTERVALS << 1)
  757.  
  758. /* If this bit is set, newline is an alternation operator.
  759.    If not set, newline is literal.  */
  760. #define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
  761.  
  762. /* If this bit is set, then `{...}' defines an interval, and \{ and \}
  763.      are literals.
  764.   If not set, then `\{...\}' defines an interval.  */
  765. #define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
  766.  
  767. /* If this bit is set, (...) defines a group, and \( and \) are literals.
  768.    If not set, \(...\) defines a group, and ( and ) are literals.  */
  769. #define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
  770.  
  771. /* If this bit is set, then \<digit> matches <digit>.
  772.    If not set, then \<digit> is a back-reference.  */
  773. #define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
  774.  
  775. /* If this bit is set, then | is an alternation operator, and \| is literal. 
  776.    If not set, then \| is an alternation operator, and | is literal.  */
  777. #define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
  778.  
  779. /* If this bit is set, then an ending range point collating higher
  780.      than the starting range point, as in [z-a], is invalid.
  781.    If not set, then when ending range point collates higher than the
  782.      starting range point, the range is ignored.  */
  783. #define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
  784.  
  785. /* If this bit is set, then an unmatched ) is ordinary.
  786.    If not set, then an unmatched ) is invalid.  */
  787. #define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
  788.  
  789. /* This global variable defines the particular regexp syntax to use (for
  790.    some interfaces).  When a regexp is compiled, the syntax used is
  791.    stored in the pattern buffer, so changing this does not affect
  792.    already-compiled regexps.  */
  793. extern reg_syntax_t re_syntax_options;
  794.  
  795. /* Define combinations of the above bits for the standard possibilities.
  796.    (The [[[ comments delimit what gets put into the Texinfo file, so
  797.    don't delete them!)  */ 
  798. /* [[[begin syntaxes]]] */
  799. #define RE_SYNTAX_EMACS 0
  800.  
  801. #define RE_SYNTAX_AWK                            \
  802.   (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL            \
  803.    | RE_NO_BK_PARENS            | RE_NO_BK_REFS                \
  804.    | RE_NO_BK_VAR               | RE_NO_EMPTY_RANGES            \
  805.    | RE_UNMATCHED_RIGHT_PAREN_ORD)
  806.  
  807. #define RE_SYNTAX_POSIX_AWK                         \
  808.   (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS)
  809.  
  810. #define RE_SYNTAX_GREP                            \
  811.   (RE_BK_PLUS_QM              | RE_CHAR_CLASSES                \
  812.    | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS                \
  813.    | RE_NEWLINE_ALT)
  814.  
  815. #define RE_SYNTAX_EGREP                            \
  816.   (RE_CHAR_CLASSES        | RE_CONTEXT_INDEP_ANCHORS            \
  817.    | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE            \
  818.    | RE_NEWLINE_ALT       | RE_NO_BK_PARENS                \
  819.    | RE_NO_BK_VBAR)
  820.  
  821. #define RE_SYNTAX_POSIX_EGREP                        \
  822.   (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES)
  823.  
  824. #define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
  825.  
  826. /* Syntax bits common to both basic and extended POSIX regex syntax.  */
  827. #define _RE_SYNTAX_POSIX_COMMON                        \
  828.   (RE_CHAR_CLASSES | RE_DOT_NEWLINE      | RE_DOT_NOT_NULL        \
  829.    | RE_INTERVALS  | RE_NO_EMPTY_RANGES)
  830.  
  831. #define RE_SYNTAX_POSIX_BASIC                        \
  832.   (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM)
  833.  
  834. /* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
  835.    RE_LIMITED_OPS, i.e., \? \+ \| are not recognized.  Actually, this
  836.    isn't minimal, since other operators, such as \`, aren't disabled.  */
  837. #define RE_SYNTAX_POSIX_MINIMAL_BASIC                    \
  838.   (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
  839.  
  840. #define RE_SYNTAX_POSIX_EXTENDED                    \
  841.   (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS            \
  842.    | RE_CONTEXT_INDEP_OPS  | RE_NO_BK_BRACES                \
  843.    | RE_NO_BK_PARENS       | RE_NO_BK_VBAR                \
  844.    | RE_UNMATCHED_RIGHT_PAREN_ORD)
  845.  
  846. /* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INVALID_OPS
  847.    replaces RE_CONTEXT_INDEP_OPS and RE_NO_BK_REFS is added.  */
  848. #define RE_SYNTAX_POSIX_MINIMAL_EXTENDED                \
  849.   (_RE_SYNTAX_POSIX_COMMON  | RE_CONTEXT_INDEP_ANCHORS            \
  850.    | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES                \
  851.    | RE_NO_BK_PARENS        | RE_NO_BK_REFS                \
  852.    | RE_NO_BK_VBAR        | RE_UNMATCHED_RIGHT_PAREN_ORD)
  853. /* [[[end syntaxes]]] */
  854.  
  855. /* Maximum number of duplicates an interval can allow.  Some systems
  856.    (erroneously) define this in other header files, but we want our
  857.    value, so remove any previous define.  */
  858. #ifdef RE_DUP_MAX
  859. #undef RE_DUP_MAX
  860. #endif
  861. #define RE_DUP_MAX ((1 << 15) - 1) 
  862.  
  863.  
  864.  
  865. /* POSIX `cflags' bits (i.e., information for `regcomp').  */
  866.  
  867. /* If this bit is set, then use extended regular expression syntax.
  868.    If not set, then use basic regular expression syntax.  */
  869. #define REG_EXTENDED 1
  870.  
  871. /* If this bit is set, then ignore case when matching.
  872.    If not set, then case is significant.  */
  873. #define REG_ICASE (REG_EXTENDED << 1)
  874.  
  875. /* If this bit is set, then anchors do not match at newline
  876.      characters in the string.
  877.    If not set, then anchors do match at newlines.  */
  878. #define REG_NEWLINE (REG_ICASE << 1)
  879.  
  880. /* If this bit is set, then report only success or fail in regexec.
  881.    If not set, then returns differ between not matching and errors.  */
  882. #define REG_NOSUB (REG_NEWLINE << 1)
  883.  
  884.  
  885. /* POSIX `eflags' bits (i.e., information for regexec).  */
  886.  
  887. /* If this bit is set, then the beginning-of-line operator doesn't match
  888.      the beginning of the string (presumably because it's not the
  889.      beginning of a line).
  890.    If not set, then the beginning-of-line operator does match the
  891.      beginning of the string.  */
  892. #define REG_NOTBOL 1
  893.  
  894. /* Like REG_NOTBOL, except for the end-of-line.  */
  895. #define REG_NOTEOL (1 << 1)
  896.  
  897. /* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
  898.  * `re_match_2' returns information about at least this many registers
  899.  * the first time a `regs' structure is passed. 
  900.  *
  901.  * Also, this is the greatest number of backreferenced subexpressions
  902.  * allowed in a pattern being matched without caller-supplied registers.
  903.  */
  904. #ifndef RE_NREGS
  905. #define RE_NREGS 30
  906. #endif
  907.  
  908. extern int rx_cache_bound;
  909.  
  910. #ifdef __STDC__
  911. extern reg_errcode_t rx_compile (const char *pattern, int size,
  912.         reg_syntax_t syntax,
  913.         struct re_pattern_buffer * rxb) ;
  914. extern int re_search_2 (struct re_pattern_buffer *rxb,
  915.          const char * string1, int size1,
  916.          const char * string2, int size2,
  917.          int startpos, int range,
  918.          struct re_registers *regs,
  919.          int stop);
  920. extern int re_search (struct re_pattern_buffer * rxb, const char *string,
  921.        int size, int startpos, int range,
  922.        struct re_registers *regs);
  923. extern int re_match_2 (struct re_pattern_buffer * rxb,
  924.         const char * string1, int size1,
  925.         const char * string2, int size2,
  926.         int pos, struct re_registers *regs, int stop);
  927. extern int re_match (struct re_pattern_buffer * rxb,
  928.       const char * string,
  929.       int size, int pos,
  930.       struct re_registers *regs);
  931. extern reg_syntax_t re_set_syntax (reg_syntax_t syntax);
  932. extern void re_set_registers (struct re_pattern_buffer *bufp,
  933.           struct re_registers *regs,
  934.           unsigned num_regs,
  935.           regoff_t * starts, regoff_t * ends);
  936. extern const char * re_compile_pattern (const char *pattern,
  937.             int length,
  938.             struct re_pattern_buffer * rxb);
  939. extern int re_compile_fastmap (struct re_pattern_buffer * rxb);
  940. extern char * re_comp (const char *s);
  941. extern int rx_exec (const char *s);
  942. extern int regcomp (regex_t * preg, const char * pattern, int cflags);
  943. extern int regexec (const regex_t *preg, const char *string,
  944.      size_t nmatch, regmatch_t pmatch[],
  945.      int eflags);
  946. extern size_t regerror (int errcode, const regex_t *preg,
  947.       char *errbuf, size_t errbuf_size);
  948. extern void regfree (regex_t *preg);
  949.  
  950. #else
  951. extern reg_errcode_t rx_compile ();
  952. extern int re_search_2 ();
  953. extern int re_search ();
  954. extern int re_match_2 ();
  955. extern int re_match ();
  956. extern reg_syntax_t re_set_syntax ();
  957. extern void re_set_registers ();
  958. extern const char * re_compile_pattern ();
  959. extern int re_compile_fastmap ();
  960. extern char * re_comp ();
  961. extern int rx_exec ();
  962. extern int regcomp ();
  963. extern int regexec ();
  964. extern size_t regerror ();
  965. extern void regfree ();
  966.  
  967. #endif
  968.  
  969.  
  970. #else /* RX_WANT_SE_DEFS */
  971.   /* Integers are used to represent side effects.
  972.    *
  973.    * Simple side effects are given negative integer names by these enums.
  974.    * 
  975.    * Non-negative names are reserved for complex effects.
  976.    *
  977.    * Complex effects are those that take arguments.  For example, 
  978.    * a register assignment associated with a group is complex because
  979.    * it requires an argument to tell which group is being matched.
  980.    * 
  981.    * The integer name of a complex effect is an index into rxb->se_params.
  982.    *
  983.    * The ordering of these effects is significant when searching.  re_se_win
  984.    * should always remain the most negative side effect.  The other values
  985.    * are less critical but the goal is that the shorter path to a win
  986.    * should always be the more negative.
  987.    */
  988.  
  989.   RX_DEF_SE(1, re_se_try, = -1)        /* Epsilon from start state */
  990.  
  991.   RX_DEF_SE(0, re_se_pushback, = re_se_try - 1)
  992.   RX_DEF_SE(0, re_se_push0, = re_se_pushback -1)
  993.   RX_DEF_SE(0, re_se_pushpos, = re_se_push0 - 1)
  994.   RX_DEF_SE(0, re_se_chkpos, = re_se_pushpos -1)
  995.   RX_DEF_SE(0, re_se_poppos, = re_se_chkpos - 1)
  996.  
  997.   RX_DEF_SE(1, re_se_at_dot, = re_se_poppos - 1)    /* Emacs only */
  998.   RX_DEF_SE(0, re_se_syntax, = re_se_at_dot - 1) /* Emacs only */
  999.   RX_DEF_SE(0, re_se_not_syntax, = re_se_syntax - 1) /* Emacs only */
  1000.  
  1001.   RX_DEF_SE(1, re_se_begbuf, = re_se_not_syntax - 1) /* match beginning of buffer */
  1002.   RX_DEF_SE(1, re_se_hat, = re_se_begbuf - 1) /* match beginning of line */
  1003.  
  1004.   RX_DEF_SE(1, re_se_wordbeg, = re_se_hat - 1) 
  1005.   RX_DEF_SE(1, re_se_wordbound, = re_se_wordbeg - 1)
  1006.   RX_DEF_SE(1, re_se_notwordbound, = re_se_wordbound - 1)
  1007.  
  1008.   RX_DEF_SE(1, re_se_wordend, = re_se_notwordbound - 1)
  1009.   RX_DEF_SE(1, re_se_endbuf, = re_se_wordend - 1)
  1010.  
  1011.   /* This fails except at the end of a line. 
  1012.    * It deserves to go here since it is typicly one of the last steps 
  1013.    * in a match.
  1014.    */
  1015.   RX_DEF_SE(1, re_se_dollar, = re_se_endbuf - 1)
  1016.  
  1017.   /* Simple effects: */
  1018.   RX_DEF_SE(1, re_se_fail, = re_se_dollar - 1) /* These are rare, = so order doesn't matter. */
  1019.   RX_DEF_SE(1, re_se_win, = re_se_fail - 1)            /* Epsilon to a final state */
  1020.  
  1021.   /* Complex effects.  These are used in the 'se' field of 
  1022.    * an RX_DEF_SE(re_se_params structure.  Indexes into the se array
  1023.    * are stored as instructions on nfa edges.
  1024.    *
  1025.    * Don't be confused that these are declared here.  They never occur in the
  1026.    * op field of a struct rx_inx.
  1027.    */
  1028.   RX_DEF_CPLX_SE(1, re_se_lparen, = 0)
  1029.   RX_DEF_CPLX_SE(1, re_se_rparen, = re_se_lparen + 1)
  1030.   RX_DEF_CPLX_SE(0, re_se_backref, = re_se_rparen + 1)
  1031.   RX_DEF_CPLX_SE(0, re_se_iter, = re_se_backref + 1) 
  1032.   RX_DEF_CPLX_SE(0, re_se_end_iter, = re_se_iter + 1)
  1033. #endif
  1034.  
  1035. #endif
  1036.